- Preface
- HTML documents
- R Code Chunks
- Markdown Basics
- R Presentation
- ioslides
- Miscellaneous
Local data scientists need better tool!
Roger D. Peng, Reproducible Research: Concepts and Ideas p.10 on Coursera
<-- rmarkdown v1 -->
RStudio — Install the latest version of RStudio
Other environments — Install the rmarkdown package.
Command line: rmarkdown::render function
rmarkdown::render("input.Rmd")Let's rock with R Markdown!
With Rstudio, You can edit various output options with friendly UI.
---
title: "R Markdown Exercise"
author: "Mansun Kuo"
date: "July 24, 2014"
output:
html_document:
css: assets/css/custom.css
fig_caption: no
fig_height: 5
fig_width: 7
highlight: default
keep_md: no
number_sections: no
theme: default
toc: yes
---
CSS:
#nextsteps {
color: blue;
}
.emphasized {
font-size: 1.2em;
}
Apply to Whole Section:
### Apply to Whole Section {#nextsteps .emphasized}
使用RStudio產生一個HTML文件:
標題為油電業薪資近9萬,是教服業的4倍?。
include table of contents
*italic* **bold** _italic_ __bold__
I am italic
I am bold
Setext:
Header 1 ============= Header 2 -------------
atx:
# Header 1 ## Header 2 ### Header 3
End a line with two or more spaces:
Roses are red, Violets are blue.
Unordered List:
* Item 1
* Item 2
+ Item 2a
- Item 2b
Ordered List:
1. Item 1
2. Item 2
3. Item 3
+ Item 3a
+ Item 3b
Unordered List:
Ordered List:
Subsequent paragraphs must be preceded by a blank line and indented four spaces or a tab.
* fruits
delicious!!!
+ apples
- macintosh
- red delicious
+ pears
+ peaches
* vegetables
healthy!!!
+ broccoli
+ chard
fruits
delicious!!!R code will be evaluated and printed
```{r}
summary(cars$dist)
```
summary(cars$dist)
## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 2 26 36 43 56 120
I counted 2 red trucks on the highway.
A plain http address or add a link to a phrase.
Inline Link:
[2014台灣資料科學愛好者年會](http://twconf.data-sci.org/)
Reference Link:
[2014台灣資料科學愛好者年會][dsc] [dsc]: http://twconf.data-sci.org/
Inline Image:

Reference Image:
![logo][R] [R]: assets/img/Taiwan-R-logo.png "R logo"
A friend once said: > It's always better to give > than to receive.
Plain code blocks are displayed in a fixed-width font but not evaulated.
```
This text is displayed verbatim / preformatted
```
Specify the language of the block is avaliable
```r
x = rnorm(10)
```
x = rnorm(10)
$equation$Display equation: $$ equation $$
<math ...> </math> for equations.Three or more asterisks or dashes:
****** ------
First Header | Second Header ------------- | ------------- Content Cell | Content Cell Content Cell | Content Cell
| First Header | Second Header |
|---|---|
| Content Cell | Content Cell |
| Content Cell | Content Cell |
加上報告的段落標題
每人每月薪資 - Top 3 & Last 3
行業別每人每月薪資總表
Named R code chunk.
```{r plot}
summary(cars)
plot(cars)
```
Easy Navigation in RStudio
echo(TRUE): whether to include R source code in the output fileeval(TRUE): whether to evaluate the code chunkmessage(TRUE): whether to preserve messages emitted by message()include(TRUE): if include=FALSE, nothing will be written into the output document, but the code is still evaluated and plot files are generatedwarning(TRUE): whether to preserve warnings in the output```{r plot, include=FALSE}
# Disable all output
```
```{r plot, echo=FALSE}
# Hide source code
```
dev('png'): figure format(png, jpeg, tiff, svg, …)fig.path('figure/'): figure pathfig.width(7): figure widthfig.height(7): figure heightdpi(72): DPI (dots per inch)```{r dev='svg', fig.path='myplot', fig.height=8}
plot(iris)
```
Set results='asis' to write raw results from R into the output document
knitr::kable
```{r, results='asis'}
knitr::kable(women)
```
xtable::xtable
```{r, results='asis'}
print(xtable::xtable(women),
type="html",
include.rownames=FALSE)
```
| height | weight |
|---|---|
| 58 | 115 |
| 59 | 117 |
| 60 | 120 |
| 61 | 123 |
| 62 | 126 |
| 63 | 129 |
cache(FALSE): whether to cache a code chunk to improve proformance for expensive computing
Caching globally for a document
```{r setup, include=FALSE}
knitr::opts_chunk$set(cache=TRUE)
```
_cache suffix.engine('R'): the language name of the code chunk
```{r engine='bash'}
whoami
```
結合適當的圖表和文字敘述, 將從每人每月平均薪資和消費者物價指數資料中觀察到的現象完成一份完整的報告。
There are various way to publish your document on the web.
Deploy your slides on the web.
Set global option in first slide.
R Presentation ======================================================== author: Mansun Kuo date: June 22, 2014
Slides automatically display their titles unless title: false is specified.
Slide 1 ==================================== title: false - Bullet 1 - Bullet 2 - Bullet 3
Two-Column Slide ==================================== left: 70% First column *** Second column
transition: transition style
transition-speed: transition speed
R Presentation ======================================================== author: Mansun Kuo date: June 22, 2014 transition: rotate transition-speed: fast
type: slide appearance
Type ======================================================== type: sub-section incremental: true
incremental: display content incrementally
Incremental Display ======================================================== incremental: true - Bullet 1 - Bullet 2 - Bullet 3
Clear Knitr Cache: clear knitr cache for this presentationView in Browser: view the presentation in an external web browserSave AS Web Page: save the presentation as a standalone web pagePublish to RPubs: publish the presentation to RPubsR Presentation ======================================================== author: Mansun Kuo date: June 22, 2014 css: assets/css/rpres.css
Apply to individual slides:
My Slide =================================== class: illustration
Apply to spans of text:
My Slide ================================== <span class="emphasized">Pay attention to this!</span>
建立一個R Presentation
#: create a section##: create a new slide---: create a new slide without a header(horizontal rule)|: add a subtitle# section ## slide 1 --- ## slide 2 | with subtitle
'f' enable fullscreen mode
'w' toggle widescreen mode
'o' enable overview mode
'h' enable code highlight mode
'p' show presenter notes
'Esc' exits all of these modes.
---
output:
ioslides_presentation:
incremental: true
---
Render bullets incrementally for specific slide:
> - Eat eggs > - Drink coffee
widescreen: widescreen modesmaller: smaller text---
output:
ioslides_presentation:
widescreen: true
smaller: true
---
Set smaller text for specific slide:
## Getting up {.smaller}
transition: default, slower, faster
---
output:
ioslides_presentation:
transition: slower
---
Let content be displayed incrementally.
## Build Slides {.build}
Add a 128x128 logo:
---
output:
ioslides_presentation:
logo: logo.png
---
Using ### <b> and ### </b> to enclose the lines you want to highlight.
x <- 10 y <- 20
To center content on a slide:
## Center {.flexbox .vcenter}
To horizontally center content:
<div class="centered">This text is centered.</div>
Note that the content will flow across the columns.
<div class="columns-2">  - Bullet 1 - Bullet 2 - Bullet 3 </div>
You can color content using base color classes red, blue, green, yellow, and gray (or variations of them e.g. red2, red3, blue2, blue3, etc.).
<div class="red2"> This text is red </div>
To enable presenter mode:
mypresentation.html?presentme=true
To disable presenter mode:
mypresentation.html?presentme=false
To add presenter notes:
<div class="notes"> This is my note. - It can contain markdown - like this list </div>
建立一個ioslides
It’s possible to embed a Shiny application within a document.
You can add R Markdown and HTML in the YAML content.
```{r results='asis', echo=FALSE}
library(whisker)
temp = '<span class="{{color}}{{number}}">{{color}}{{number}}</span>'
numbers = c("", "2", "3")
colors = c("red", "blue", "green", "yellow", "gray")
for (color in colors){
cat("- ")
for (number in numbers){
out = whisker.render(temp)
cat(out)
}
cat("\n")
}
```
library(knitr)
library(ggvis)
mtcars %>%
ggvis(x = ~wt, y = ~mpg) %>%
layer_smooths(se=TRUE, opacity := 0.5, opacity.hover := 0.75) %>%
layer_points(fill = ~factor(cyl), size := 50, size.hover := 200) %>%
set_options(hover_duration = 250)